package org.scnu.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadFileTxt {

    public static  List<String[]> readFileByLines(String fileName) throws FileNotFoundException {  
        File file = new File(fileName);  
        BufferedReader reader = null;
        List<String[]>  points = new ArrayList<>();
         try {  
            System.out.println("读取行：");  
            reader = new BufferedReader(new FileReader(ReadFileTxt.class.getClassLoader().getResource(fileName).getFile()));  
            String tempString = null;  
            int line = 1;           
            // 一次读入一行，直到读入null为文件结束  
            while ((tempString = reader.readLine()) != null) {                  
            	String tempString1=tempString.replaceAll("\\s", "");
            	System.out.println(tempString1);
            	Pattern pattern = Pattern.compile("[0-9]{1,}");
                Matcher matcher = pattern.matcher((CharSequence)tempString1);
                boolean result=matcher.matches();            
                 System.out.println(result);
                 if(result){
            	tempString=tempString+"\r\n";  
                String[] segments =  tempString.split("\\s{2,}|\t"); 
                
                String[] xy =new String[2];
                for(int i=0;i<2;i++){
                	xy[i]=segments[i];
                }    
            	points.add(xy);    
                 }
                System.out.println("line " + line + ":" + tempString);  
                line++;  
            }  
            reader.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                try {  
                    reader.close();                     
                    System.out.println("内部方法打印(ReadFileTxt(line-44)--->>文件行数："+points.size());
                    //打印每行数据
                    for(int i=0;i<points.size();i++){                  	
                    	for(int j=0;j<points.get(i).length;j++){                 		
                    		System.out.print(points.get(i)[j]+" ");            	                   	
                    	}
                    	//System.out.println(points.get(i).length);
                    	System.out.println();
                    }         
                } catch (IOException e1) {  
                }  
            }  
        }
		return points;  
    } 
	
    

    

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		String filename ="data/Compound.txt";
		List<String[]>  points=readFileByLines(filename);
		String[] p1;
		String[] p2;
		//测试打印距离矩阵
	for(int i=0;i<points.size();i++){
        	for(int j=i+1;j<points.size();j++){     
        			p1=new String[points.get(i).length];
        			p2=new String[points.get(i).length];
        			p1=points.get(i);
        			p2=points.get(j);
        			System.out.println("i ："+i+p1+" ");      		
        			System.out.println("j："+j+p2+" ");              	          	                   	
        	}
        }
		
	String tempString1="820 50";
	String tempString=tempString1.replaceAll("\\s", "");
	System.out.println(tempString);
	Pattern pattern = Pattern.compile("[0-9]{1,}");
    Matcher matcher = pattern.matcher((CharSequence)tempString);
    boolean result=matcher.matches();
    System.out.println("result:"+result);
		
		
		
/*		List<String[]> list = new ArrayList();
		String[] sb = new String[]{"1","2"};
		String[] sb1 = new String[]{"3","4"};
		
		list.add(sb);
		list.add(sb1);
		
		
		
		for(int i=0;i<list.size();i++){
			for(int j=0;j<list.get(i).length;j++){
				System.out.println(list.get(i).length);
			System.out.println(list.get(i)[j]);
			}
		}*/	
	}
}